/**
 * 弹窗基类
 * Name = uiBase
 * DateTime = Sun Jan 16 2022 00:49:14 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = uiBase.ts
 * gitee： https://gitee.com/coyeking
 */

 import { _decorator, Component, Node, tween, Vec3 } from 'cc';
 import { poolManager } from '../framework/poolManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('UiBase')
 export class UiBase extends Component {
 
     @property(Node)
     content: Node = undefined;
 
     protected _isOnload: boolean = false;
 
     onLoad() {
         this._isOnload = true;
         this.openAnim();
     }
     
    onEnable() {
        if (this._isOnload) {
            this._isOnload = false;
            return;
        }
        this.openAnim();
    }
 
    /**
     * 
     * @param cb 关闭完成返回
     * @param isAct 是否动画
     */
    protected closeAction(cb?: Function, isAct: boolean = true) {
        if (isAct) {
            let animTime = 0.3;
            tween(this.content).to(animTime, { scale: new Vec3() }, { easing: 'backIn' }).call(() => {
            poolManager.instance.putNode(this.node);
                if (cb) cb();
            }).start();
        }
        else {
        poolManager.instance.putNode(this.node);
        }
    }

    /**
     * 
     * @param cb 打开完成回调
     * @param isAct 打开动画
     */
    protected openAnim(cb?: Function, isAct: boolean = true) {
        this.node.active = true;
        let animTime = 0.3;
        if (isAct) {
            this.content.scale = new Vec3();
            tween(this.content).to(animTime, { scale: new Vec3(1, 1, 1) }, { easing: 'backOut' }).call(() => {
                if (cb) cb();
            }).start();
        }
        else {
            this.content.scale = new Vec3(1, 1, 1);
        }
    }
 }
 

